import Popup from "../btoolkit/components/Popup";
import BButton from "../btoolkit/components/BButton";
import SDKManager from "../btoolkit/sdk/SDKManager";
import EventManager from "../btoolkit/EventManager";
import PopupAlert from "../btoolkit/components/PopupAlert";
import App from "../btoolkit/framework/App";
import EventNames from "../global/EventNames";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PopupShareTip extends Popup {

    @property(cc.Node)
    btnShare: cc.Node = null;

    @property(cc.Label)
    label: cc.Label = null;

    static instance: PopupShareTip = null;

    static show(onClose: Function) {
        if (PopupShareTip.instance) {
            console.error('不能重复创建PopupShareTip');
            return;
        }
        PopupShareTip.instance = cc.instantiate(App.instance.popupShareTip).getComponent(PopupShareTip);
        cc.Canvas.instance.node.addChild(PopupShareTip.instance.node);
        PopupShareTip.instance.onClose = onClose;
    }

    onDestroy() {
        super.onDestroy();
        PopupShareTip.instance = null;
    }

    onLoad() {
        super.onLoad();
        this.label.string = SDKManager.sharedTimes + '';

        BButton.addButton(this.btnShare).setTouchUpEvent(() => {
            SDKManager.instance.share((over3seconds: boolean) => {
                if (over3seconds) {
                    this.node.destroy();
                    SDKManager.sharedTimes += 1;
                    if (SDKManager.sharedTimes >= 5) {
                        SDKManager.instance.hideBanner(false);
                        EventManager.emit(EventNames.DisableAllAD);
                        PopupAlert.show('恭喜你，广告已去除');
                    }
                }
            });
        });
    }

}
